﻿//////////////////////////////////////////////////////////////////////////////////
//
// Project            : SimpleProxySwitch
// Module             : SchedulerCustomAction
// Description        : Custom action for scheduler.
// 
// Repository         : $URL$
// Last changed by    : $LastChangedBy$
// Revision           : $LastChangedRevision$
// Last Changed       : $LastChangedDate$
// Author             : $Author$
//
// Id                 : $Id$
//
// Copyright          : (c) 2010 Torsten Bär
//
// Published under the MIT License. See license.txt or http://www.opensource.org/licenses/mit-license.php.
//
//////////////////////////////////////////////////////////////////////////////////
using System;
using System.Linq;
using Microsoft.Deployment.WindowsInstaller;
using Microsoft.Win32.TaskScheduler;

namespace SchedulerCustomAction
{
   /// <summary>
   /// Custom installer action for scheduler.
   /// </summary>
   public class ScheduleCustomAction
   {
      /// <summary>
      /// Deletes the schedule.
      /// </summary>
      /// <param name="session">The session.</param>
      /// <returns>The <see cref="ActionResult"/>.</returns>
      [CustomAction]
      public static ActionResult DeleteSchedule(Session session)
      {
         session.Log("Begin DeleteSchedule");

         using (TaskService taskService = new TaskService())
         {
            session.Log("TaskService initialized");

            using (Task task = taskService.RootFolder.Tasks.FirstOrDefault(t => t.Name == "ProxyUpdate"))
            {
               if (task != null)
               {
                  session.Log("Task found");

                  taskService.RootFolder.DeleteTask(task.Name);

                  session.Log("Task deleted");
               }
               else
               {
                  session.Log("No task found");
               }
            }
         }

         session.Log("End DeleteSchedule");

         return ActionResult.Success;
      }
   }
}
